package com.huika.dealontop.control.pay.utils;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;

import com.android.volley.Response;
import com.google.gson.reflect.TypeToken;
import com.hk.app.android.lib.http.VolleyRequestManager;
import com.huika.dealontop.R;
import com.huika.dealontop.config.Constants;
import com.huika.dealontop.config.UrlConstants;
import com.huika.dealontop.control.base.BaseAct;
import com.huika.dealontop.support.domain.LinePayInfoBean;
import com.huika.dealontop.support.http.JSONWrapAjaxParams;
import com.huika.dealontop.support.http.LinePayHttp.LineRequestResult;
import com.huika.dealontop.support.http.LinePayHttp.LineResultRequest;

/**
 * Description:Line Pay
 * Created by zhouweiyong on 2015/11/23.
 */
public class LinePay extends BasePay {
    private BaseAct baseAct;
    private String reserveUrl;
    private String linePayChannelId;//Line Pay渠道ID
    private String linePayChannelSecret;//LinePay渠道密钥
    private int linePaySupportedVersion = 230;

    public LinePay(BaseAct baseAct) {
        this.baseAct = baseAct;
        if (UrlConstants.isReleaseEnvironment()) {
            linePayChannelId = "1443610143";
            linePayChannelSecret = "1768150c41edb61bf11b8769a173dcd8";
            reserveUrl = "https://api-pay.line.me/v2/payments/request";
        } else {
            linePayChannelId = "1443610447";
            linePayChannelSecret = "23009dae120944136d23c5cd93426139";
            reserveUrl = "https://sandbox-api-pay.line.me/v2/payments/request";
        }
    }

    @Override
    public void pay() {
        reserveNet();
    }

    private void reserveNet() {
        JSONWrapAjaxParams ajaxParams = new JSONWrapAjaxParams();
        ajaxParams.putCommonTypeParam("productName", Constants.PRODUCTNAME);
        ajaxParams.putCommonTypeParam("productImageUrl", "http://www.0066mall.com/Content/css/images/logo-th.png");
        ajaxParams.putCommonTypeParam("amount", getTotalMoney());
        ajaxParams.putCommonTypeParam("currency", Constants.CURRENCY);
        ajaxParams.putCommonTypeParam("confirmUrl", "http://www.dealontop.com/payment/LinePayConfirm");
        ajaxParams.putCommonTypeParam("orderId", getPayOrderNo());
        LineResultRequest<LinePayInfoBean> request = new LineResultRequest<LinePayInfoBean>(reserveUrl, new Response.Listener<LineRequestResult<LinePayInfoBean>>() {

            @Override
            public void onResponse(LineRequestResult<LinePayInfoBean> arg0) {
                confirmPay(arg0.getInfo().getPaymentUrl().getApp());
            }
        }, baseAct, new TypeToken<LineRequestResult<LinePayInfoBean>>() {
        }.getType());
        request.setRequestParams(ajaxParams);
        VolleyRequestManager.addRequest(request, this);
    }

    private void confirmPay(String paymentUrl){
        try {
            PackageManager pm = baseAct.getPackageManager();
            PackageInfo packageInfo = pm.getPackageInfo("jp.naver.line.android", 0);
            int versionCode = packageInfo.versionCode;
            if (linePaySupportedVersion <= versionCode) {
                launchUri(paymentUrl);
            } else {
                confirmLineInstall(baseAct);
            }
        }  catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
    }

    private void confirmLineInstall(Context context) {
        new AlertDialog.Builder(context)
                .setTitle("LINE Pay")
                .setMessage(baseAct.getString(R.string.linepay_confirm))
                .setCancelable(false)
                .setPositiveButton(baseAct.getString(R.string.linepay_install), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        launchUri("market://details?id=jp.naver.line.android");
                    }
                })
                .setNegativeButton(baseAct.getString(R.string.linepay_cancel), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    }
                })
                .show();
    }

    private void launchUri(String uriString) {
        Uri uri = Uri.parse(uriString);
        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
        baseAct.startActivity(intent);
    }
}
